package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.EmployeeOutpass;
import com.amsp.oa.oademo.mapper.EmployeeOutpassMapper;
import com.amsp.oa.oademo.service.EmployeeOutpassService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.List;

@Service
public class EmployeeOutpassServiceImpl extends ServiceImpl<EmployeeOutpassMapper, EmployeeOutpass>
        implements EmployeeOutpassService {

    @Override
    public IPage<EmployeeOutpass> getPage(Integer pageNum, Integer pageSize, String holderName, String employeeNumber) {
        LambdaQueryWrapper<EmployeeOutpass> queryWrapper = new LambdaQueryWrapper<>();

        if (StringUtils.hasText(holderName)) {
            queryWrapper.like(EmployeeOutpass::getHolderName, holderName);
        }

        if (StringUtils.hasText(employeeNumber)) {
            queryWrapper.like(EmployeeOutpass::getEmployeeNumber, employeeNumber);
        }

        queryWrapper.orderByDesc(EmployeeOutpass::getCreateTime);

        return page(new Page<>(pageNum, pageSize), queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatchTransactional(List<EmployeeOutpass> outpassList) {
        return super.saveOrUpdateBatch(outpassList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByIdsTransactional(List<Long> ids) {
        return removeByIds(ids);
    }
}